move some code in a shared awk script, add fast replacement functions for [ -z ]...
authorFelix Fietkau <[email protected]>
Sun, 9 Oct 2005 21:06:35 +0000 (21:06 +0000)
committerFelix Fietkau <[email protected]>
Sun, 9 Oct 2005 21:06:35 +0000 (21:06 +0000)
SVN-Revision: 2088

15 files changed:
openwrt/package/webif/files/usr/lib/webif/categories.awk [new file with mode: 0644]
openwrt/package/webif/files/usr/lib/webif/common.awk [new file with mode: 0644]
openwrt/package/webif/files/usr/lib/webif/display-dhcp.awk [new file with mode: 0644]
openwrt/package/webif/files/usr/lib/webif/display-hosts.awk [new file with mode: 0644]
openwrt/package/webif/files/usr/lib/webif/form.awk
openwrt/package/webif/files/usr/lib/webif/subcategories.awk [new file with mode: 0644]
openwrt/package/webif/files/usr/lib/webif/webif.sh
openwrt/package/webif/files/www/cgi-bin/webif/config.sh
openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh
openwrt/package/webif/files/www/cgi-bin/webif/index.sh
openwrt/package/webif/files/www/cgi-bin/webif/lan.sh
openwrt/package/webif/files/www/cgi-bin/webif/system.sh
openwrt/package/webif/files/www/cgi-bin/webif/upgrade.sh
openwrt/package/webif/files/www/cgi-bin/webif/wan.sh
openwrt/package/webif/files/www/cgi-bin/webif/wireless-config.sh

diff --git a/openwrt/package/webif/files/usr/lib/webif/categories.awk b/openwrt/package/webif/files/usr/lib/webif/categories.awk
new file mode 100644 (file)
index 0000000..26dcf39
--- /dev/null
@@ -0,0 +1,27 @@
+BEGIN {
+       n = 0
+       sel = 0
+       FS=":"
+}
+($3 == "category") && (categories !~ /:$4:/) {
+       categories = categories ":" $4 ":";
+       n++
+       if ($4 ~ "^" selected "$") sel = n
+       c[n] = $4
+       if (f[$4] == "") f[$4] = rootdir "/" indexpage "?cat=" $4
+}
+($3 == "name") && ((p[$4] == 0) || (p[$4] > int($5))) {
+       gsub(/^.*\//, "", $1);
+       p[$4] = int($5)
+       f[$4] = rootdir "/" $1
+}
+END {
+       print "<div id=\"mainmenu\"><h3><strong>Categories:</strong></h3><ul>"
+       
+       for (i = 1; i <= n; i++) {
+               if (sel == i) print "<li class=\"selected-maincat\"><a href=\"" f[c[i]] "\">&raquo;" c[i] "&laquo;</a></li>"
+               else print "<li><a href=\"" f[c[i]] "\">&nbsp;" c[i] "&nbsp;</a></li>";
+       }
+  
+       print "</ul></div>"
+}
diff --git a/openwrt/package/webif/files/usr/lib/webif/common.awk b/openwrt/package/webif/files/usr/lib/webif/common.awk
new file mode 100644 (file)
index 0000000..d7e50e2
--- /dev/null
@@ -0,0 +1,12 @@
+function start_form(title, field_opts) {
+       print "<div class=\"settings\">"
+       if (title != "") print "<div class=\"settings-title\"><h3><strong>" title "</strong></h3></div>"
+       print "<div class=\"settings-content\">"
+}
+
+function end_form(form_help, form_help_link) {
+       print "</div>"
+       if (form_help != "") form_help = "<dl>" form_help "</dl>"
+       print "<div class=\"settings-help\"><blockquote><h3><strong>Short help:</strong></h3>" form_help form_help_link "</blockquote></div>"
+       print "<div style=\"clear: both\">&nbsp;</div></div>"
+}
diff --git a/openwrt/package/webif/files/usr/lib/webif/display-dhcp.awk b/openwrt/package/webif/files/usr/lib/webif/display-dhcp.awk
new file mode 100644 (file)
index 0000000..28c5067
--- /dev/null
@@ -0,0 +1,21 @@
+BEGIN {
+       FS="[ \t]"
+       print "<form enctype=\"multipart/form-data\" method=\"post\">"
+       start_form("Static IP addresses (for DHCP)")
+       print "<table width=\"70%\" summary=\"Settings\">"
+       print "<tr><th>MAC address</th><th>IP</th><th></th></tr>"
+}
+
+# only for valid MAC addresses
+($1 ~ /^[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]$/) {
+       gsub(/#.*$/, "");
+       print "<tr><td>" $1 "</td><td>" $2 "</td><td align=\"right\" width=\"10%\"><a href=\"" url "?remove_dhcp=1&remove_mac=" $1 "\">Remove</a></td></tr>"
+}
+
+END {
+       print "<tr><td><input type\"text\" name=\"dhcp_mac\" value=\"" mac "\" /></td><td><input type=\"text\" name=\"dhcp_ip\" value=\"" ip "\" /></td><td style=\"width: 10em\"><input type=\"submit\" name=\"add_dhcp\" value=\"Add\" /></td></tr>"
+       print "</table>"
+       print "</form>"
+       end_form();
+}
+
diff --git a/openwrt/package/webif/files/usr/lib/webif/display-hosts.awk b/openwrt/package/webif/files/usr/lib/webif/display-hosts.awk
new file mode 100644 (file)
index 0000000..db522a3
--- /dev/null
@@ -0,0 +1,37 @@
+BEGIN {
+       FS="[ \t]"
+       start_form("Hostnames")
+       print "<table width=\"70%\" summary=\"Settings\">"
+       print "<tr><th>IP</th><th>Hostname</th><th></th></tr>"
+       print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
+}
+
+# only for valid IPv4 addresses
+($1 ~ /^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/) {
+       gsub(/#.*$/, "");
+       output = ""
+       names_found = 0
+       n = split($0, names, "[ \t]")
+       first = 1
+       for (i = 2; i <= n; i++) {
+               if (names[i] != "") {
+                       if (first != 1) output = output "<tr>"
+                       output = output "<td>" names[i] "</td><td align=\"right\" width=\"10%\"><a href=\"" url "?remove_host=1&remove_ip=" $1 "&remove_name=" names[i] "\">Remove</a></td></tr>"
+                       first = 0
+                       names_found++
+               }
+       }
+       if (names_found > 0) {
+               print "<tr><td rowspan=\"" names_found "\">" $1 "</td>" output
+               print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
+       }
+}
+
+END {
+       print "<form enctype=\"multipart/form-data\" method=\"post\">"
+       print "<tr><td><input type\"text\" name=\"host_ip\" value=\"" ip "\" /></td><td><input type=\"text\" name=\"host_name\" value=\"" name "\" /></td><td style=\"width: 10em\"><input type=\"submit\" name=\"add_host\" value=\"Add\" /></td></tr>"
+       print "</form>"
+       print "</table>"
+       end_form()
+}
+
index 8a3d110e72ba357ac31e89ee6bcdbd527405d948..c6bf747a4a6d62fbafc2900830992bb1c47dcb5f 100644 (file)
@@ -22,9 +22,8 @@ $1 ~ /^start_form/ {
        if ($3 != "") field_opts=" id=\"" $3 "\""
        else field_opts=""
        if ($4 == "hidden") field_opts = field_opts " style=\"display: none\""
-       print "<div class=\"settings\"" field_opts ">"
-       if ($2 != "") print "<div class=\"settings-title\"><h3><strong>" $2 "</strong></h3></div>"
-       print "<div class=\"settings-content\"><table width=\"100%\" summary=\"Settings\">"
+       start_form($2, field_opts);
+       print "<table width=\"100%\" summary=\"Settings\">"
        form_help = ""
        form_help_link = ""
 }
@@ -78,10 +77,8 @@ $1 ~ /^helplink/ { form_help_link = "<div class=\"more-help\"><a href=\"" $2 "\"
 $1 ~ /^end_form/ {
        if (field_open == 1) print "</td></tr>"
        field_open = 0
-       print "</table></div>"
-       if (form_help != "") form_help = "<dl>" form_help "</dl>"
-       print "<div class=\"settings-help\"><blockquote><h3><strong>Short help:</strong></h3>" form_help form_help_link "</blockquote></div>"
+       print "</table>"
+       end_form(form_help, form_help_link);
        form_help = ""
        form_help_link = ""
-       print "<div style=\"clear: both\">&nbsp;</div></div>"
 }
diff --git a/openwrt/package/webif/files/usr/lib/webif/subcategories.awk b/openwrt/package/webif/files/usr/lib/webif/subcategories.awk
new file mode 100644 (file)
index 0000000..b748de2
--- /dev/null
@@ -0,0 +1,12 @@
+BEGIN {
+       FS=":"
+       print "<div id=\"submenu\"><h3><strong>Sub-Categories:</strong></h3><ul>"
+}
+{
+       if ($5 ~ "^" selected "$") print "<li class=\"selected-maincat\"><a href=\"" rootdir "/" $6 "\">&raquo;" $5 "&laquo;</a></li>"
+       else print "<li><a href=\"" rootdir "/" $6 "\">&nbsp;" $5 "&nbsp;</a></li>"
+}
+END {
+       print "</ul></div>"
+}
+
index 9de03b955b7aceae7450eb4f2f5ccc0ee01b8105..cd8115b7e43f835cd1d23a8da37384b7c0ff530a 100644 (file)
@@ -4,49 +4,35 @@ cgidir=/www/cgi-bin/webif
 rootdir=/cgi-bin/webif
 indexpage=index.sh
 
+# workarounds for stupid busybox fork/exec on [ ]
+empty() {
+       case "$1" in
+               "") return 0 ;;
+               *) return 255 ;;
+       esac
+}
+equal() {
+       case "$1" in
+               "$2") return 0 ;;
+               *) return 255 ;;
+       esac
+}
+
 categories() {
-       grep '##WEBIF:' $cgidir/.categories $cgidir/*.sh 2>/dev/null | awk -F: '
-       BEGIN {
-               n = 0
-               sel = 0
-       }
-       ($3 == "category") && (categories !~ /:$4:/) {
-               categories = categories ":" $4 ":";
-               n++
-               if ($4 ~ /^'"$1"'$/) sel = n
-               c[n] = $4
-               if (f[$4] == "") f[$4] = "'"$rootdir/$indexpage"'?cat=" $4
-       }
-       ($3 == "name") && ((p[$4] == 0) || (p[$4] > int($5))) {
-               gsub(/^.*\//, "", $1);
-               p[$4] = int($5)
-               f[$4] = "'"$rootdir"'/" $1
-       }
-       END {
-               print "<div id=\"mainmenu\"><h3><strong>Categories:</strong></h3><ul>"
-               
-               for (i = 1; i <= n; i++) {
-                       if (sel == i) print "<li class=\"selected-maincat\"><a href=\"" f[c[i]] "\">&raquo;" c[i] "&laquo;</a></li>"
-                       else print "<li><a href=\"" f[c[i]] "\">&nbsp;" c[i] "&nbsp;</a></li>";
-               }
-         
-               print "</ul></div>"
-       }' -
+       grep '##WEBIF:' $cgidir/.categories $cgidir/*.sh 2>/dev/null | \
+               awk -v "selected=$1" \
+                       -v "rootdir=$rootdir" \
+                       -v "indexpage=$indexpage" \
+                       -f /usr/lib/webif/categories.awk -
 }
 
 subcategories() {
-       grep -H "##WEBIF:name:$1:" $cgidir/*.sh 2>/dev/null | sed -e 's,^.*/\([a-zA-Z\.\-]*\):\(.*\)$,\2:\1,' | sort -n | awk -F: '
-       BEGIN {
-      print "<div id=\"submenu\"><h3><strong>Sub-Categories:</strong></h3><ul>"
-       }
-       {
-         if ($5 ~ /^'"$2"'$/) print "<li class=\"selected-maincat\"><a href=\"'"$rootdir/"'" $6 "\">&raquo;" $5 "&laquo;</a></li>"
-         else print "<li><a href=\"'"$rootdir/"'" $6 "\">&nbsp;" $5 "&nbsp;</a></li>"
-       }
-       END {
-      print "</ul></div>"
-       }
-  ' -
+       grep -H "##WEBIF:name:$1:" $cgidir/*.sh 2>/dev/null | \
+               sed -e 's,^.*/\([a-zA-Z\.\-]*\):\(.*\)$,\2:\1,' | \
+               sort -n | \
+               awk -v "selected=$2" \
+                       -v "rootdir=$rootdir" \
+                       -f /usr/lib/webif/subcategories.awk -
 }
 
 update_changes() {
@@ -54,8 +40,14 @@ update_changes() {
 }
 
 header() {
-       ERROR=${ERROR:+<h3>$ERROR</h3><br /><br />}
-       SAVED=${SAVED:+: Settings saved}
+       empty "$ERROR" && {
+               _saved_title="${SAVED:+: Settings saved}"
+       } || {
+               FORM_submit="";
+               ERROR="<h3>$ERROR</h3><br /><br />"
+               _saved_title=": Settings not saved"
+       }
+
        _category="$1"
        _uptime="$(uptime)"
        _loadavg="${_uptime#*load average: }"
@@ -64,8 +56,6 @@ header() {
        _hostname=$(cat /proc/sys/kernel/hostname)
        _version=$(cat /etc/banner | grep "(")
        _version="${_version%% ---*}"
-       _saved_title=${ERROR:+: Settings not saved}
-       _saved_title=${_saved_title:-$SAVED}
        _head="${3:+<div class=\"settings-block-title\"><h2>$3$_saved_title</h2></div>}"
        _form="${5:+<form enctype=\"multipart/form-data\" action=\"$5\" method=\"post\"><input type="hidden" name="submit" value="1" />}"
        _savebutton="${5:+<p><input type=\"submit\" name=\"action\" value=\"Save changes\" /></p>}"
@@ -106,9 +96,8 @@ Pragma: no-cache
                                $_head
                                $ERROR
 EOF
-       [ -z "$REMOTE_USER" \
-         -a "${SCRIPT_NAME#/cgi-bin/}" != "webif.sh" ] && {
-               [ -z $FORM_passwd1 ] || {
+       empty "$REMOTE_USER" && equal "${SCRIPT_NAME#/cgi-bin/}" "webif.sh" && {
+               empty "$FORM_passwd" && {
                        echo '<pre>'
                        (
                                echo "$FORM_passwd1"
@@ -193,7 +182,7 @@ apply_passwd() {
 }
 
 display_form() {
-       echo "$1" | awk -F'|' -f /usr/lib/webif/form.awk
+       echo "$1" | awk -F'|' -f /usr/lib/webif/common.awk -f /usr/lib/webif/form.awk
 }
 
 list_remove() {
@@ -209,26 +198,34 @@ BEGIN {
 }
 
 handle_list() {
-       _new="${1:+$(list_remove "$LISTVAL" "$1") }"
-       _new="${_new:-$LISTVAL}"
-       LISTVAL="$_new"
-       LISTVAL="${LISTVAL# }"
-       LISTVAL="${LISTVAL%% }"
+       # $1 - remove
+       # $2 - add
+       # $3 - submit
+       # $4 - validate
        
-       _validate="$4"
-       _validate="${4:-none}"
-       _changed="$1"
-       [ \! -z "$3" ] && validate "$_validate|$2" && {
-               LISTVAL="$LISTVAL $2"
-               _changed="$1$3"
+       empty "$1" || {
+               LISTVAL="$(list_remove "$LISTVAL" "$1") "
+               LISTVAL="${LISTVAL# }"
+               LISTVAL="${LISTVAL%% }"
+               _changed=1
+       }
+       
+       empty "$3" || {
+               validate "${4:-none}|$2" && {
+                       LISTVAL="$LISTVAL $2"
+                       _changed=1
+               }
        }
 
-       _return="${_changed:+0}"
-       _return="${_return:-255}"
        LISTVAL="${LISTVAL# }"
        LISTVAL="${LISTVAL%% }"
        LISTVAL="${LISTVAL:- }"
-       return $_return
+
+       if empty "$_changed"; then
+               return 255
+       else
+               return 0
+       fi
 }
 
 load_settings() {
@@ -240,6 +237,7 @@ validate() {
        eval "$(echo "$1" | awk -f /usr/lib/webif/validate.awk)"
 }
 
+
 save_setting() {
        mkdir -p /tmp/.webif
        oldval=$(eval "echo \${$2}")
@@ -249,23 +247,8 @@ save_setting() {
                grep -v "^$2=" /tmp/.webif/config-$1-old > /tmp/.webif/config-$1 2>&- 
                oldval=""
        }
-       [ "$oldval" != "$3" ] && echo "$2=\"$3\"" >> /tmp/.webif/config-$1
+       equal "$oldval" "$3" || echo "$2=\"$3\"" >> /tmp/.webif/config-$1
        rm -f /tmp/.webif/config-$1-old
 }
 
 
-# common awk code for forms
-AWK_START_FORM='
-       print "<div class=\"settings\">"
-       print "<div class=\"settings-title\"><h3><strong>" title "</strong></h3></div>"
-       print "<div class=\"settings-content\">"
-'
-AWK_END_FORM='
-       print "</div>"
-       if (form_help != "") form_help = "<dl>" form_help "</dl>"
-       print "<div class=\"settings-help\"><blockquote><h3><strong>Short help:</strong></h3>" form_help form_help_link "</blockquote></div>"
-       form_help = ""
-       form_help_link = ""
-       print "<div style=\"clear: both\">&nbsp;</div></div>"
-'
-
index 254884fba441549f562243c389879f37daaf2458..b024561203c4b2f506946bfd8b9e83fc9eb9b1a0 100755 (executable)
@@ -35,10 +35,9 @@ case "$FORM_mode" in
        save)
                header $FORM_cat . "Configuration: updating..."
                CHANGES=""
-               echo '<pre>'
+               echo "<pre>"
                sh /usr/lib/webif/apply.sh
-               echo '</pre>'
-               echo "${FORM_prev:+<meta http-equiv=\"refresh\" content=\"2; URL=$FORM_prev\" />}"
+               echo "</pre>${FORM_prev:+<meta http-equiv=\"refresh\" content=\"2; URL=$FORM_prev\" />}"
                ;;
 esac
 
index 5faa4885c64f867d02bebb01c031ec89b29de792..72c7c62176b055addbf0b0c7836cc16c6bb2e55f 100755 (executable)
@@ -58,88 +58,30 @@ update_ethers() {
        ETHERS_FILE=/tmp/.webif/file-ethers
 }
 
-[ ! -z "$FORM_add_host" ] && {
+empty "$FORM_add_host" || {
        # add a host to /etc/hosts
        validate "ip|FORM_host_ip|IP Address|required|$FORM_host_ip
 hostname|FORM_host_name|Hostname|required|$FORM_host_name" && update_hosts add "$FORM_host_ip" "$FORM_host_name"
 }
-[ ! -z "$FORM_add_dhcp" ] && {
+empty "$FORM_add_dhcp" || {
        # add a host to /etc/ethers
        validate "mac|FORM_dhcp_mac|MAC Address|required|$FORM_dhcp_mac
 ip|FORM_dhcp_ip|IP|required|$FORM_dhcp_ip" && update_ethers add "$FORM_dhcp_mac" "$FORM_dhcp_ip"
 }
-[ ! -z "$FORM_remove_host" ] && update_hosts del "$FORM_remove_ip" "$FORM_remove_name"
-[ ! -z "$FORM_remove_dhcp" ] && update_ethers del "$FORM_remove_mac"
+empty "$FORM_remove_host" || update_hosts del "$FORM_remove_ip" "$FORM_remove_name"
+empty "$FORM_remove_dhcp" || update_ethers del "$FORM_remove_mac"
 
 header "Network" "Hosts" "Configured hosts" ''
 
 # Hosts in /etc/hosts
-# FIXME: move formatting code in form.awk if possible
-awk -v "url=$SCRIPT_NAME" '
-BEGIN {
-       FS="[ \t]"
-       title = "Hostnames"
-       '"$AWK_START_FORM"'
-       print "<table width=\"70%\" summary=\"Settings\">"
-       print "<tr><th>IP</th><th>Hostname</th><th></th></tr>"
-       print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
-}
-
-# only for valid IPv4 addresses
-($1 ~ /^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/) {
-       gsub(/#.*$/, "");
-       output = ""
-       names_found = 0
-       n = split($0, names, "[ \t]")
-       first = 1
-       for (i = 2; i <= n; i++) {
-               if (names[i] != "") {
-                       if (first != 1) output = output "<tr>"
-                       output = output "<td>" names[i] "</td><td align=\"right\" width=\"10%\"><a href=\"" url "?remove_host=1&remove_ip=" $1 "&remove_name=" names[i] "\">Remove</a></td></tr>"
-                       first = 0
-                       names_found++
-               }
-       }
-       if (names_found > 0) {
-               print "<tr><td rowspan=\"" names_found "\">" $1 "</td>" output
-               print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
-       }
-}
-
-END {
-       print "<form enctype=\"multipart/form-data\" method=\"post\">"
-       print "<tr><td><input type\"text\" name=\"host_ip\" value=\"'"$FORM_host_ip"'\" /></td><td><input type=\"text\" name=\"host_name\" value=\"'"$FORM_host_name"'\" /></td><td style=\"width: 10em\"><input type=\"submit\" name=\"add_host\" value=\"Add\" /></td></tr>"
-       print "</form>"
-       print "</table>"
-       '"$AWK_END_FORM"'
-}
-' $HOSTS_FILE
+awk -v "url=$SCRIPT_NAME" \
+       -v "ip=$FORM_host_ip" \
+       -v "name=$FORM_host_name"  -f /usr/lib/webif/common.awk -f /usr/lib/webif/display-hosts.awk $HOSTS_FILE
 
 # Static DHCP mappings (/etc/ethers)
-# FIXME: move formatting code in form.awk if possible
-awk -v "url=$SCRIPT_NAME" '
-BEGIN {
-       FS="[ \t]"
-       title = "Static IP addresses (for DHCP)"
-       '"$AWK_START_FORM"'
-       print "<form enctype=\"multipart/form-data\" method=\"post\">"
-       print "<table width=\"70%\" summary=\"Settings\">"
-       print "<tr><th>MAC address</th><th>IP</th><th></th></tr>"
-}
-
-# only for valid MAC addresses
-($1 ~ /^[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]$/) {
-       gsub(/#.*$/, "");
-       print "<tr><td>" $1 "</td><td>" $2 "</td><td align=\"right\" width=\"10%\"><a href=\"" url "?remove_dhcp=1&remove_mac=" $1 "\">Remove</a></td></tr>"
-}
-
-END {
-       print "<tr><td><input type\"text\" name=\"dhcp_mac\" value=\"'"$FORM_dhcp_mac"'\" /></td><td><input type=\"text\" name=\"dhcp_ip\" value=\"'"$FORM_dhcp_ip"'\" /></td><td style=\"width: 10em\"><input type=\"submit\" name=\"add_dhcp\" value=\"Add\" /></td></tr>"
-       print "</table>"
-       print "</form>"
-       '"$AWK_END_FORM"'
-}
-' - < $ETHERS_FILE
+awk -v "url=$SCRIPT_NAME" \
+       -v "mac=$FORM_dhcp_mac" \
+       -v "ip=$FORM_dhcp_ip" -f /usr/lib/webif/common.awk -f /usr/lib/webif/display-dhcp.awk $ETHERS_FILE
 
 footer ?>
 <!--
index 6a9a8f58a221533b11ca542096c6329698cbe0f0..3ab14d61dac369943eca0486c412ed70642d8840 100755 (executable)
@@ -2,7 +2,7 @@
 <? 
 . /usr/lib/webif/webif.sh
 category=$FORM_cat
-[ -z "$category" ] && category=Info
+empty "$category" && category=Info
 header $category 1
-?>
-<? footer ?>
+
+footer ?>
index d9fd7759e6ab4c1d4adbf99336557c672010c633..8ce03b4b570a9c502f69fd84bfebac9d71438162 100755 (executable)
@@ -11,12 +11,11 @@ handle_list "$FORM_dnsremove" "$FORM_dnsadd" "$FORM_dnssubmit" 'ip|FORM_dnsadd|L
 }
 FORM_dnsadd=${FORM_dnsadd:-192.168.1.1}
 
-
-if [ -z "$FORM_submit" -o \! -z "$ERROR" ]; then
+if empty "$FORM_submit"; then 
        FORM_lan_ipaddr=${lan_ipaddr:-$(nvram get lan_ipaddr)}
        FORM_lan_netmask=${lan_netmask:-$(nvram get lan_netmask)}
        FORM_lan_gateway=${lan_gateway:-$(nvram get lan_gateway)}
-else
+else 
        SAVED=1
        validate "
 ip|FORM_lan_ipaddr|LAN IP|required|$FORM_lan_ipaddr
index f90ee756a1e037596057a8ca8a482a5fa4b1a499..212b3d65fa5ba7dba6153c2d1914d4c2593dfd1c 100755 (executable)
@@ -4,14 +4,14 @@
 load_settings system
 load_settings nvram
 
-[ -z $FORM_submit ] && {
+if empty "$FORM_submit"; then
        FORM_hostname=${wan_hostname:-$(nvram get wan_hostname)}
        FORM_hostname=${FORM_hostname:-OpenWrt}
        grep BCM947 /proc/cpuinfo 2>&- >&- && {
                FORM_boot_wait=${boot_wait:-$(nvram get boot_wait)}
                FORM_boot_wait=${FORM_boot_wait:-off}
        }
-} || {
+else
        SAVED=1
        validate "hostname|FORM_hostname|Hostname|nodots required|$FORM_hostname" && {
                save_setting system wan_hostname $FORM_hostname
@@ -21,7 +21,7 @@ load_settings nvram
                        esac
                }
        }
-}
+fi
 
 header "System" "Settings" "System settings" '' "$SCRIPT_NAME"
 
@@ -37,6 +37,7 @@ field
 end_form"
 
 footer ?>
+
 <!--
 ##WEBIF:name:System:1:Settings
 -->
index 6de13d330939b7d001fb0656445ee10cad6575b5..6b5fe9a166e9a051e12e4d7e71ebe2bbad76534a 100755 (executable)
@@ -8,7 +8,7 @@ strip_cybertan() {
        rm $FORM_firmware
 }
 
-[ -z "$FORM_submit" -o -z "$FORM_firmware" ] || {
+empty "$FORM_submit" || empty "$FORM_firmware" || {
        [ -n $FORM_firmware ] && {
                HEADER=$(head -c4 $FORM_firmware | hexdump -e "8/1 \"%x\"")
                grep BCM947 /proc/cpuinfo > /dev/null && {
@@ -35,7 +35,7 @@ strip_cybertan() {
        }
 }
 ?>
-<?if [ -z "$UPGRADE" ] ?>
+<?if empty "$UPGRADE" ?>
        <form method="POST" name="upgrade" action="<? echo -n $SCRIPT_NAME ?>" enctype="multipart/form-data">
        <table style="width: 90%; text-align: left;" border="0" cellpadding="2" cellspacing="2" align="center">
        <tbody>
index 60a930de296075e72d63134dd06463be2968649c..1d6202906c2ec1e8b1523c0806de2e1f145bafdb 100755 (executable)
@@ -13,7 +13,7 @@ handle_list "$FORM_dnsremove" "$FORM_dnsadd" "$FORM_dnssubmit" 'ip|FORM_dnsadd|W
 FORM_dnsadd=${FORM_dnsadd:-192.168.1.1}
 
 
-[ -z $FORM_submit ] && {
+if empty "$FORM_submit"; then
        FORM_wan_proto=${wan_proto:-$(nvram get wan_proto)}
        case "$FORM_wan_proto" in
                # supported types
@@ -46,21 +46,17 @@ text|pptp_server_ip|$FORM_pptp_server_ip"
 
        redial=${ppp_demand:-$(nvram get ppp_demand)}
        case "$redial" in
-               1|enabled|on)
-                       FORM_ppp_redial="demand"
-               ;;      
-               *)
-                       FORM_ppp_redial="persist"
-               ;;      
+               1|enabled|on) FORM_ppp_redial="demand";;        
+               *) FORM_ppp_redial="persist";;  
        esac
        
        FORM_pptp_server_ip=${pptp_server_ip:-$(nvram get pptp_server_ip)}
-} || {
+else
        SAVED=1
 
-       [ -z $FORM_wan_proto ] && {
+       empty "$FORM_wan_proto" && {
                ERROR="No WAN protocol selected" 
-               return -1
+               return 255
        }
 
        case "$FORM_wan_proto" in
@@ -83,41 +79,41 @@ ip|FORM_pptp_server_ip|PPTP server IP|$V_PPTP|$FORM_pptp_server_ip" && {
                
                # Settings specific to one protocol type
                case "$FORM_wan_proto" in
-                       static)
-                               save_setting network wan_gateway $FORM_wan_gateway
-                               ;;
-                       pptp)
-                               save_setting network pptp_server_ip "$FORM_pptp_server_ip"
-                               ;;
+                       static) save_setting network wan_gateway $FORM_wan_gateway ;;
+                       pptp) save_setting network pptp_server_ip "$FORM_pptp_server_ip" ;;
                esac
                
                # Common settings for PPTP, Static and DHCP 
-               [ "$FORM_wan_proto" = "pptp" -o "$FORM_wan_proto" = "static" -o "$FORM_wan_proto" = "dhcp" ] && {
-                       save_setting network wan_ipaddr $FORM_wan_ipaddr
-                       save_setting network wan_netmask $FORM_wan_netmask 
-               }
+               case "$FORM_wan_proto" in
+                       pptp|static|dhcp)
+                               save_setting network wan_ipaddr $FORM_wan_ipaddr
+                               save_setting network wan_netmask $FORM_wan_netmask 
+                       ;;
+               esac
                
                # Common PPP settings
-               [ "$FORM_wan_proto" = "pppoe" -o "$FORM_wan_proto" = "pptp" ] && {
-                       [ -z $FORM_ppp_username ] || save_setting network ppp_username $FORM_ppp_username
-                       [ -z $FORM_ppp_passwd ] || save_setting network ppp_passwd $FORM_ppp_passwd
-       
-                       # These can be blank
-                       save_setting network ppp_idletime $FORM_ppp_idletime
-                       save_setting network ppp_redialperiod $FORM_ppp_redialperiod
-                       save_setting network ppp_mtu $FORM_ppp_mtu
-       
-                       case "$FORM_ppp_redial" in
-                               demand)
-                                       save_setting network ppp_demand 1
-                                       ;;
-                               persist)
-                                       save_setting network ppp_demand ""
-                                       ;;
-                       esac    
-               }
+               case "$FORM_wan_proto" in
+                       pppoe|pptp)
+                               empty "$FORM_ppp_username" || save_setting network ppp_username $FORM_ppp_username
+                               empty "$FORM_ppp_passwd" || save_setting network ppp_passwd $FORM_ppp_passwd
+               
+                               # These can be blank
+                               save_setting network ppp_idletime "$FORM_ppp_idletime"
+                               save_setting network ppp_redialperiod "$FORM_ppp_redialperiod"
+                               save_setting network ppp_mtu "$FORM_ppp_mtu"
+               
+                               case "$FORM_ppp_redial" in
+                                       demand)
+                                               save_setting network ppp_demand 1
+                                               ;;
+                                       persist)
+                                               save_setting network ppp_demand ""
+                                               ;;
+                               esac    
+                       ;;
+               esac
        }
-}
+fi
 
 header "Network" "WAN" "WAN settings" ' onLoad="modechange()" ' "$SCRIPT_NAME"
 ?>
index e0f8a72022a8e391de2d8f65844f5f0241e4f55d..36bf1b4473885d61dd98ba97661ffb4f845bccb4 100755 (executable)
@@ -22,7 +22,8 @@ for ch in $CHANNELS; do
 "
 done
 
-if [ -z "$FORM_submit" -o \! -z "$ERROR" ]; then
+
+if empty "$FORM_submit"; then
        FORM_mode=${wl0_mode:-$(nvram get wl0_mode)}
        infra=${wl0_infra:-$(nvram get wl0_infra)}
        case "$infra" in
@@ -80,7 +81,7 @@ if [ -z "$FORM_submit" -o \! -z "$ERROR" ]; then
                        FORM_tkip=tkip
                        ;;
        esac
-       [ $FORM_encryption = off ] && {
+       equal "$FORM_encryption" off && {
                wep=${wl0_wep:-$(nvram get wl0_wep)}
                case "$wep" in
                        1|enabled|on) FORM_encryption=wep;;
@@ -95,8 +96,11 @@ if [ -z "$FORM_submit" -o \! -z "$ERROR" ]; then
        FORM_key=${key:-1}
 else
        SAVED=1
-       [ "$FORM_encryption" = "wpa" ] && V_RADIUS="required"
-       [ "$FORM_encryption" = "psk" ] && V_PSK="required"
+       case "$FORM_encryption" in
+               wpa) V_RADIUS="required";;
+               psk) V_PSK="required";;
+       esac
+
        validate "
 ip|FORM_radius_ipaddr|RADIUS IP address|$V_RADIUS|$FORM_radius_ipaddr
 wep|FORM_key1|WEP key 1||$FORM_key1
@@ -107,24 +111,21 @@ string|FORM_wpa_psk|WPA pre-shared key|min=8 max=63 $V_PSK|$FORM_wpa_psk
 string|FORM_radius_key|RADIUS server key|min=4 max=63 $V_RADIUS|$FORM_radius_key
 string|FORM_ssid|ESSID|required|$FORM_ssid
 int|FORM_channel|Channel|required min=1 max=$CHANNEL_MAX|$FORM_channel" && {
-               case "$FORM_mode" in
-                       adhoc)
-                               save_setting wireless wl0_mode sta
-                               save_setting wireless wl0_infra 0
-                               ;;
-                       *)
-                               save_setting wireless wl0_mode "$FORM_mode"
-                               save_setting wireless wl0_infra 1
-                               ;;
-               esac
+
+               if equal "$FORM_mode" adhoc; then
+                       FORM_mode=sta
+                       infra="0"
+               fi
+               save_setting wireless wl0_mode "$FORM_mode"
+               save_setting wireless wl0_infra ${infra:-1}
                        
                save_setting wireless wl0_ssid "$FORM_ssid"
                save_setting wireless wl0_channel "$FORM_channel"
-               case "$FORM_aes$FORM_tkip" in 
-                       aes) save_setting wireless wl0_crypto aes;;
-                       tkip) save_setting wireless wl0_crypto tkip;;
-                       aestkip) save_setting wireless wl0_crypto tkip+aes;;
-               esac
+       
+               crypto=""
+               equal "$FORM_aes" aes && crypto="aes"
+               equal "$FORM_tkip" tkip && crypto="tkip${crypto:++$crypto}"
+
                case "$FORM_encryption" in
                        psk)
                                case "${FORM_wpa1}${FORM_wpa2}" in
@@ -163,7 +164,8 @@ int|FORM_channel|Channel|required min=1 max=$CHANNEL_MAX|$FORM_channel" && {
 fi
 
 header "Network" "Wireless" "Wireless settings" ' onLoad="modechange()" ' "$SCRIPT_NAME"
-?>
+
+cat <<EOF
 <script type="text/javascript" src="/webif.js"></script>
 <script type="text/javascript">
 <!--
@@ -207,7 +209,9 @@ function modechange()
 -->
 </script>
 
-<? display_form "start_form|Wireless Configuration
+EOF
+
+display_form "start_form|Wireless Configuration
 field|ESSID
 text|ssid|$FORM_ssid
 helpitem|ESSID